//
//  AFCustomObject.m
//  F50
//
//  Created by 郝 巍 on 15/4/30.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "AFCustomObject.h"

@implementation AFCustomObject


+ (id)shareAFCustomObject {
    
    static AFCustomObject *customObject;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        customObject = [[AFCustomObject alloc] init];
    });
    return customObject;
}
- (void)doRequestActionWithParams:(NSDictionary*)dict
                          success:(void (^)(NSDictionary *result))successBlock
                             fail:(void (^)())failBlock{
    [self connectWithParams:dict
                    httpUrl:@""
               isPostMethod:YES
                    success:^(NSDictionary *result) {
                        
                        if (successBlock) {
                            successBlock(result);
                        }
                    } fail:^{
                        if (failBlock) {
                            failBlock();
                        }
                    }];
}
- (void)loginActionWithParams:(NSDictionary*)dict
                      success:(void (^)(NSDictionary *result))successBlock
                         fail:(void (^)())failBlock {
    
    [self connectWithParams:dict
                    httpUrl:@""
               isPostMethod:YES
                    success:^(NSDictionary *result) {
                        
                        if (successBlock) {
                            successBlock(result);
                        }
                    } fail:^{
                        if (failBlock) {
                            failBlock();
                        }
                    }];
}

- (void)registrAtionWithParams:(NSDictionary*)dict
                       success:(void (^)(NSDictionary *result))successBlock
                          fail:(void (^)())failBlock{
    
    [self connectWithParams:dict
                    httpUrl:@""
               isPostMethod:YES
                    success:^(NSDictionary *result) {
                        
                        if (successBlock) {
                            successBlock(result);
                        }
                    } fail:^{
                        if (failBlock) {
                            failBlock();
                        }
                    }];
    
}


- (void)uploadImgWithParams:(NSDictionary*)dict
            withUploadImage:(UIImage *)image
                    success:(void (^)(NSDictionary *result))successBlock
                       fail:(void (^)())failBlock{
   
    
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    AFJSONResponseSerializer *responseSerializer= [AFJSONResponseSerializer serializer];
    //申明返回的结果是json类型
    [manager setResponseSerializer:responseSerializer];
    //申明请求的数据是json类型
//    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = \
    [NSSet setWithObjects:@"application/json", @"text/html", @"text/javascript", nil]; //如果报接受类型不一致请替换一致text/html或别的
   
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:dict];
    // 转换image为data数据
     NSData *imageData = UIImageJPEGRepresentation(image, 0.3);
    
    [manager POST:@"http://f50.favtouch.com/index.php?s=api&c=view&a=uploadfile" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
          [formData appendPartWithFileData:imageData name:@"file" fileName:@"pp.jpg" mimeType:@"image/jpeg"];
        }
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        requestTmp = [requestTmp stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        //系统自带JSON解析
        responseObject = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        
        id result = responseObject;
        if (successBlock) {
            successBlock(result);
        }

        }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error.localizedDescription);
        NSLog(@"%@", operation.response);
        if ([operation responseObject] != nil) {
            
            if (failBlock) {
                failBlock();
            }
        }

    }];
    
    
    
    
    
}



#pragma mark
#pragma mark - 统一请求方法
- (void)connectWithParams:(id)parameters
                  httpUrl:(NSString*)url
             isPostMethod:(BOOL)ispost
                  success:(void (^)(NSDictionary *result))successBlock
                     fail:(void (^)())failBlock
{
    
//    AFHTTPRequestOperationManager *manager = \
//    [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:API_HOST]];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    AFJSONResponseSerializer *responseSerializer= [AFJSONResponseSerializer serializer];
    //申明返回的结果是json类型
    [manager setResponseSerializer:responseSerializer];
    //申明请求的数据是json类型
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = \
    [NSSet setWithObjects:@"application/json", @"text/html", @"text/javascript", nil]; //如果报接受类型不一致请替换一致text/html或别的
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:parameters];
  
    if (ispost) {
    
        [manager POST:API_HOST
           parameters:dict
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSLog(@"%@",operation.response);
                  
                  NSString *requestTmp = [NSString stringWithString:operation.responseString];
                  requestTmp = [requestTmp stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
                  NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
                  //系统自带JSON解析
                  responseObject = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
                  
                  
                  id result = responseObject;
                  if (successBlock) {
                      
                      successBlock(result);
                  }
                  
              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"%@", error.localizedDescription);
                  NSLog(@"%@", operation.response);
                  if ([operation responseObject] != nil) {
                      
                      if (failBlock) {
                          failBlock();
                      }
                  }
              }];
    }
    else {
        [manager GET:url
          parameters:dict
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 
                 id result = responseObject;
                 
                 if (successBlock) {
                     
                     successBlock(result);
                 }
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 
                 NSLog(@"%@", error.localizedDescription);
                 if (failBlock) {
                     failBlock();
                 }
             }];
    }
}

@end
